#pragma once

#include "avilib.h"
//#include "MediaStream.h"

#define MAX_VIDEO_SIZE  (1024*512)

namespace host {
    class AviFile
    {
    public:
        AviFile();
        virtual ~AviFile();

        operator FILE*() { return m_hAviFd; }
        operator int() { return m_hAviFd == nullptr; };

        bool IsOpen() { return m_hAviFd != nullptr; };
        bool SetAenc(int i_nChan = 1, int i_nSamplerate = 32000, int i_nBitWidth = 16);
        bool SetVenc(int i_nWidth, int i_nHeight, int i_nFps = 30, int i_nGop = 0, const char* i_pCompressor = "MJPG");
        bool Create(const char* i_sPath, bool i_bVenc = true, bool i_bAenc = false);
        bool Open(const char* i_sPath);
        bool Close();
        bool WriteVideo(unsigned char* i_pData, unsigned int i_u32Len, unsigned int i_u32Timestamp);
        bool WriteTail();

    protected:

    private:
        bool CreatAVIFile(const char* i_sPath);
        unsigned int AVICalcAlign(unsigned int u32Data, unsigned char u8Align);

    private:
        bool         m_bVenc;
        unsigned int m_u32Width;
        unsigned int m_u32Height;
        double       m_u64fps;
        unsigned int m_u32Gop;
        char         m_pCompressor[8];

        bool         m_bAenc;
        unsigned int m_u32Channels;
        unsigned int m_u32Rate;
        unsigned int m_u32Bits;
        unsigned int m_u32Format;

        FILE*       m_hAviFd;
        AVI_PARA_S  m_stPara;
        unsigned char* m_pVideoBuff;
        unsigned int   m_u32MoviLen;
    };

};
